package com.zhdl.modules.electricitymeter;

import cn.hutool.json.JSONUtil;
import com.zhdl.common.util.WebResult;
import com.zhdl.network.electricityMeter.DeviceInfo;
import com.zhdl.network.electricityMeter.LiveData;
import com.zhdl.network.electricityMeter.ThresholdInfo;
import com.zhdl.network.service.WaterMeterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.zhdl.network.servertcp.ModBusTcpServerHandler.ipMaps;

@Slf4j
@RestController
@RequestMapping(path = "/electricityMeter", produces = MediaType.APPLICATION_JSON_VALUE)
public class ElectricityMeterController {

    public DeviceInfo deviceInfo = new DeviceInfo();

//    @GetMapping("/addDevice")
//    public WebResult<String> addDevice(){
//        String jsonStr = JSONUtil.toJsonStr(ipMaps);
//        return WebResult.okResult(jsonStr);
//    }
//    @GetMapping("/checkDevice")
//    public WebResult<String> checkDevice(){
//        String jsonStr = JSONUtil.toJsonStr(ipMaps);
//        return WebResult.okResult(jsonStr);
//    }
    public LiveData liveData = new LiveData();
    public ThresholdInfo thresholdInfo = new ThresholdInfo();
    @Resource
    WaterMeterService waterMeterService;
    String result = "";

    @GetMapping("/getDeviceInfo")
    public WebResult<String> getDeviceInfo() {
        String jsonStr = JSONUtil.toJsonStr(ipMaps);
        return WebResult.okResult(jsonStr);
    }

    /**
     * 电表项目-发送指令1
     */
    @GetMapping("/sendQuery")
    public WebResult<String> sendQuery(int id, int cmd) {

        waterMeterService.selectCmd(id, cmd, o -> {
            switch (cmd) {
                case 1 -> {
                    deviceInfo = (DeviceInfo) o;
                }
                case 2 -> {
                    liveData = (LiveData) o;
                }
                case 3, 4, 5, 6 -> {
                    result = (String) o;
                }
                case 7 -> {
                    thresholdInfo = (ThresholdInfo) o;
                }
                default -> throw new IllegalStateException("Unexpected value: " + cmd);
            }
        });
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        if (cmd == 1) {
            String jsonStr = JSONUtil.toJsonStr(deviceInfo);
            return WebResult.okResult("result:" + jsonStr);
        } else if (cmd == 2) {
            String jsonStr = JSONUtil.toJsonStr(liveData);
            return WebResult.okResult("result:" + jsonStr);
        } else if (cmd == 7) {
            String jsonStr = JSONUtil.toJsonStr(thresholdInfo);
            return WebResult.okResult("result:" + jsonStr);
        } else {
            String jsonStr = JSONUtil.toJsonStr(result);
            return WebResult.okResult("result:" + jsonStr);
        }
    }

    @GetMapping("/sendQuery1")
    public WebResult<String> sendQuery() {
        waterMeterService.sendWaterMeter1("1");
        return WebResult.okResult("Send message to " + ", send cmd " + "success!");
    }
    //        switch (cmd){
//            case 1 -> {
//                waterMeterService.sendWaterMeter1(string);
//            }
//            case 2 -> {
//                waterMeterService.sendWaterMeter2(string);
//            }
//            case 3 -> {
//                waterMeterService.sendWaterMeter3(string);
//            }
//            case 4 -> {
//                waterMeterService.sendWaterMeter4(string);
//            }
//            case 5 -> {
//                waterMeterService.sendWaterMeter5(string);
//            }
//            case 6 -> {
//                waterMeterService.sendWaterMeter6(string);
//            }
//            case 7 -> {
//                waterMeterService.sendWaterMeter7(string);
//            }
//            default ->
//                log.info("未匹配到对应的id");
//        }

}
